using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Core.Toolkit.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class CBS_OutFactoryToolService : BaseAppService<CBS_OutFactoryToolDto, CBS_OutFactoryTool, CBS_OutFactoryToolRepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly UserIdentity _userIdentity;
        public CBS_OutFactoryToolService(CBS_OutFactoryToolRepository repository, IHttpContextAccessor accessor, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
            if (accessor.HttpContext != null)
            {
                _userIdentity = accessor.HttpContext.User.GetUserIdentity();
            }
        }



        private LoadPageOffsetModelAuto<CBS_OutFactoryTool, CBS_OutFactoryToolDto>
            GetLoadPageOffsetMode(PagingModel<CBS_OutFactoryToolDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("IntoFactoryToolId", "=") //
     .Add("OutCarryingQuantity", "=") //
     .Add("SituationStatement", "=") //
     .Add("DepartureTime", "=") //
     .Add("FactoryConfirmer", "=") //
     .Add("CreateFactory", "=") //
     .Add("CreateDept", "=") //
     .Add("UpdateDept", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new CBS_OutFactoryTool()
            {
                Id = model.Id,
                IntoFactoryToolId = model.IntoFactoryToolId,//
                OutCarryingQuantity = model.OutCarryingQuantity,//
                SituationStatement = model.SituationStatement,//
                DepartureTime = model.DepartureTime,//
                FactoryConfirmer = model.FactoryConfirmer,//
                CreateFactory = model.CreateFactory,//
                CreateDept = model.CreateDept,//
                UpdateDept = model.UpdateDept,//
            };
            loadPageOffsetModel.BasqQuery = loadPageOffsetModel.BasqQuery.OrderByDescending(x => x.CreateTime).Where(y => y.CreateFactory == _userIdentity.Factory);
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<CBS_OutFactoryToolDto>> GetList(PagingModel<CBS_OutFactoryToolDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<CBS_OutFactoryToolDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.IntoFactoryToolId,
                IntoFactoryToolId_title = "",
                model.OutCarryingQuantity,
                OutCarryingQuantity_title = "",
                model.SituationStatement,
                SituationStatement_title = "",
                model.DepartureTime,
                DepartureTime_title = "",
                model.FactoryConfirmer,
                FactoryConfirmer_title = "",
                model.CreateFactory,
                CreateFactory_title = "",
                model.CreateDept,
                CreateDept_title = "",
                model.UpdateDept,
                UpdateDept_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}